package adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.qianfeng.lingshijia.DetailsActivity;
import com.qianfeng.lingshijia.R;

import java.util.ArrayList;
import java.util.List;

import custom.ImportView;
import entity.HeadEntity;
import entity.TotalList_Import_Entity;

/**
 * Created by Mr.xingkong on 2016/1/12.
 * 进口散装ListView适配器
 */
public class ImportListAdapter<T> extends BaseAdapter implements View.OnClickListener {
    List<T> data;
    private Context context;
    int positions = 0 ;
    int productID = 0;
    public ImportListAdapter(Context context)
    {
        this.context = context;
        data = new ArrayList<>();
    }

    public void setData( List<T> data)
    {
        this.data = data;
        notifyDataSetChanged();
    }

    public void addData(List<T> data)
    {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData(){
        data.clear();
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        positions = position;
        ImportView importView = new ImportView(context);
        convertView = importView;
        String title,cname,referral,price,Imgurl,Iconurl;
        if (data.get(position) instanceof TotalList_Import_Entity.DataEntity.PListEntity)
        {
             title = (( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getTitle();
             cname = (( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getOriginName();
             referral =(( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getShortDesc();
             price= (( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getPriceStr();
             Imgurl =(( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getImgUrl();
             Iconurl = (( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getIconUrl();
             productID = (( TotalList_Import_Entity.DataEntity.PListEntity)(data.get(position))).getProductId();
        }else
        {
             title = ((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getTitle();
             cname = ((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getOriginName();
             referral =((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getShortDesc();
             price= ((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getPriceStr();
             Imgurl =((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getImgUrl();
             Iconurl = ((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getIconUrl();
             productID =  ((HeadEntity.DataEntity.ImportBulkProductListEntity)(data.get(position))).getProductId();
        }
        convertView.setOnClickListener(this);
        importView.shoppingCart_iv_import.setOnClickListener(this);
        importView.setData(Imgurl,Iconurl,title,cname,referral,price);
        return convertView;
    }

    @Override
    public void onClick(View v)
    {
        if (v.getId() == R.id.shoppingCart_iv_import)
        {
            Toast.makeText(context, "放入购物车", Toast.LENGTH_SHORT).show();
        }else{
            Intent intent = new Intent();
            intent.setClass(context, DetailsActivity.class);
            intent.putExtra("productId",productID);
            context.startActivity(intent);
        }
    }
}
